package com.company.socket.client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;    
import java.io.DataOutputStream;     
import java.io.FileOutputStream;
import java.net.Socket;       
import java.util.concurrent.ExecutorService;    
import java.util.concurrent.Executors;


	
public class TransferClient {   
		
	        
//		private String receiveFilePath = Constants.RECEIVE_FILE_PATH;   
	        
	    /**  
	     * 带参数的构造器，用户设定需要传送文件的文件夹  
	     * @param filePath  
	     */    
	    public TransferClient(String filePath){    
	            
	    }    
	        
	    /**  
	     * 不带参数的构造器。使用默认的传送文件的文件夹  
	     * @throws Exception 
	     */    
	    public TransferClient() throws Exception{  
	        
	    }    
	        
	    public void service(){    
	        ExecutorService executorService = Executors.newCachedThreadPool();    
//	        Vector<Integer> vector = getRandom(fileList.size());    
//	        for(Integer integer : vector){    
//	            String filePath = fileList.get(integer.intValue());    
//	            executorService.execute(sendFile(filePath));    
//	        }  
	        String fileName = "test.txt";
	        executorService.execute(receiveFile(fileName)); 
	      
	    }    
	        
	               
	        
	    private static Runnable receiveFile(final String fileName){    
	        return new Runnable(){    
	                
	            private Socket socket = null;    
	            private String ip ="localhost";    
	            private int port = 10000;    
	                
	            public void run() {    
	                System.out.println("开始发送文件:" + fileName);  
	                if(createConnection()){  //链接服务器
	                	
	                	DataInputStream dis = null;    
			            DataOutputStream fos = null;    
			            DataOutputStream dos = null;
			    
			            int bufferSize = 8192;    
			            byte[] buf = new byte[bufferSize];
						System.out.println("ok1");
			            try {    
			                dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));    
			                dos = new DataOutputStream(socket.getOutputStream());  

			                dos.writeUTF(fileName);//写入后阻塞了

			                //写入文件名
			                dos.flush();
							System.out.println("ok2");
							long length = dis.readLong();
							System.out.println(length);
			                dis.readUTF();
			                String savePath = Constants.RECEIVE_FILE_PATH + dis.readUTF();    
			                fos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(savePath)));
							System.out.println("ok2");
			                int read = 0;    
			                long passedlen = 0;    
			                while ((read = dis.read(buf)) != -1) {    
			                    passedlen += read;    
			                    fos.write(buf, 0, read);    
			                    System.out.println("文件[" + savePath + "]已经接收: " + passedlen * 100L/ length + "%");    
			                }    
			                System.out.println("文件: " + savePath + "接收完成!");    
			                    
			            } catch (Exception e) {    
			                e.printStackTrace();    
			                System.out.println("接收文件失败!");    
			            }finally{    
			                try {    
			                    if(fos != null){    
			                        fos.close();    
			                    }    
			                    if(dis != null){    
			                        dis.close();    
			                    }    
			                    if(socket != null){    
			                        socket.close();    
			                    }    
			                } catch (Exception e) {    
			                    e.printStackTrace();    
			                }    
			            }
	                }
	            }    
	                
	            private boolean createConnection() {    
	                try {    
	                    socket = new Socket(ip, port);    
	                    System.out.println("连接服务器成功！");    
	                    return true;    
	                } catch (Exception e) {    
	                    System.out.println("连接服务器失败！");    
	                    return false;    
	                }     
	            }    
	                
	        };    
	    }

	  
	        
	    public static void main(String[] args) throws Exception{    
	         new TransferClient().service();  
	    } 
}
